﻿using FlightTicketContract;
using FlightTicketModel;
using HttpOperation.Crawl.HttpCrawl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using WebServices.Responses;
using Newtonsoft.Json.Converters;
using flyticketBase;

namespace FlightTicketClient
{
    class Program
    {
        static string apiaddress = System.Configuration.ConfigurationManager.AppSettings["apiaddress"];
        static IFlightTicket _localclient = new FlightTicketApi.Controllers.FlithTicket.FlightTicketController();
        static void Main(string[] args)
        {
            ChannelFactory<IFlightTicket> channelFactory = new ChannelFactory<IFlightTicket>("Request");
            var client = channelFactory.CreateChannel();
            string ip = _localclient.NetIP();

            var coo = new System.Net.CookieContainer();
            HttpRequest req = new HttpRequest();
            var tsks = req.CollectHtml($"http://localhost:80/apir/FlightTicket/GetSpiderTask?ip={ip}&count=10", "", "GET", HttpOperation.Crawl.Entity.HtmlEncoding.UTF8, HttpOperation.Crawl.Entity.HtmlEncoding.UTF8, ref coo);
            var response = Newtonsoft.Json.JsonConvert.DeserializeObject<ResponsePageDataBase<FlightTicketChildTask>>(tsks);
            var tag = new WebServices.WebServices.Requests.RequestStatus() { ids = new List<long>(), Status = FlightTicketModel.TaskStatus.ClientReceived };
            var ids = from i in response.data
                      select i.Id;
            tag.ids = ids.ToList();
            tag.Status = FlightTicketModel.TaskStatus.ClientReceived;
            var resp = client.UpdateTasksStatus(tag);
            SchedulerTask _task = new FlightTicketClient.SchedulerTask(client);
            ResponseTag info = new ResponseTag();
            foreach (var item in response.data)
            {
                var res = _task.WebRequest(item, out info);

                switch (info.errcode)
                {
                    case ErrCode.None:
                        break;
                    case ErrCode.Normal:
                        break;
                    case ErrCode.NotData:
                        break;
                    case ErrCode.HasErr:
                        break;
                    case ErrCode.Fatal:
                        break;
                    case ErrCode.Forbidden:
                        client.ForbiddenIP(ip, item.Airline.Domain.Trim());
                        break;
                    default:
                        break;
                }
            }
        }

        private static void HttpTest(string ip)
        {
            var coo = new System.Net.CookieContainer();
            HttpRequest req = new HttpRequest();
            var tsks = req.CollectHtml($"http://localhost:80/apir/FlightTicket/GetSpiderTask?ip={ip}&count=10", "", "GET", HttpOperation.Crawl.Entity.HtmlEncoding.UTF8, HttpOperation.Crawl.Entity.HtmlEncoding.UTF8, ref coo);
            var response = Newtonsoft.Json.JsonConvert.DeserializeObject<ResponsePageDataBase<FlightTicketChildTask>>(tsks);
            var tag = new WebServices.WebServices.Requests.RequestStatus() { ids = new List<long>(), Status = FlightTicketModel.TaskStatus.ClientReceived };
            var ids = from i in response.data
                      select i.Id;
            tag.ids = ids.ToList();
            tag.Status = FlightTicketModel.TaskStatus.ClientReceived;
            _localclient.UpdateTasksStatus(tag);
        }

        static void WCFTest()
        {
            ChannelFactory<IFlightTicket> channelFactory = new ChannelFactory<IFlightTicket>("Request");
            var client = channelFactory.CreateChannel();
            SchedulerTask schedulerTask = new SchedulerTask(client);
            string ip = client.NetIP();
            while (true)
            {

                var taskrep = client.GetSpiderTask(ip, 10);

                Console.WriteLine(taskrep.error);
                var k = Console.ReadKey();
                if (k.Key == ConsoleKey.Escape)
                    break;
            }
        }
    }
}
